import {Post} from "para-lib";
import {API_CONFIG} from "../../config/api.config";
import {SetGlobalLoadingActions} from "./settingAction";
import {AUTH} from "../types";
import {Decrypt} from "../../utils/crypto";
/* 登出 */
export const authLogOutActions = (dispatch: any) => async () => {
    dispatch({type: AUTH.LOGOUT, payload: {hasLogin: false, token: ""}});
    window.localStorage.removeItem("plcconsole");
};
/* 通过手机号登录、H5注册通用 */
export const AuthLoginByPhoneActions = (dispatch: any, params: any) => async () => {
    dispatch(SetGlobalLoadingActions(dispatch, true));
    const {data, err} = await Post({
        url: API_CONFIG.User.login,
        data: {
            phone: Decrypt(params.mobile),
            password: Decrypt(params.password)
        }
    });
    dispatch(SetGlobalLoadingActions(dispatch, false));
    if (err) return false;
    dispatch(SetLoginInfoAction(dispatch, params));
    dispatch({type: AUTH.LOGIN, payload: {hasLogin: true, token: data?.data?.access_token}});
};

// 记住密码和手机的账户信息
export const SetLoginInfoAction = (dispatch: any, params: any) => async () => {
    dispatch({type: AUTH.LOGIN_INFO, payload: params});
};
